// CAccountInfoMenuItem.cpp : 实现文件
//

#include "AccountInfoMenu.h"

#include <QColor>
#include <QPainter>
#include <QRect>

#include "core/StyleMgr.h"

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// class CAccountInfoMenuItem
CAccountInfoMenuItem::CAccountInfoMenuItem() {
  m_strTitle = "";
  m_nID = -1;
}

CAccountInfoMenuItem::~CAccountInfoMenuItem() {}

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// class CAccountInfoMenu
namespace {
const int nGap = 5;
}
CAccountInfoMenu::CAccountInfoMenu() {
  m_nIDHover = m_nIDPos = m_nIDSelect = -1;
}

CAccountInfoMenu::~CAccountInfoMenu() {}

void CAccountInfoMenu::SetFont(QFont& lfHead, QFont& lfContent) {
  m_lfHead = lfHead;
  m_lfContent = lfContent;
}

void CAccountInfoMenu::SetSelect(int nID) {
  for (QVector<CAccountInfoMenuItem>::iterator it = m_Menu.begin();
       it != m_Menu.end(); it++) {
    if (it->m_nID == nID) {
      m_nIDSelect = nID;
      return;
    }
    for (QVector<CAccountInfoMenuItem>::iterator it2 = it->m_SubMenu.begin();
         it2 != it->m_SubMenu.end(); it2++) {
      if (it2->m_nID == nID) {
        m_nIDSelect = nID;
        return;
      }
    }
  }
  m_nIDSelect = -1;
  return;
}

void CAccountInfoMenu::DrawMenu(QPainter* painter) {
  QColor brBack = NAME_COLOR("BasicPlate");
  QColor brBackHover = NAME_COLOR("Hover");
  QColor brBackSelect = NAME_COLOR("PartPlate");
  QColor brFront = NAME_COLOR("TextNormal");
  QColor brText = NAME_COLOR("Highlight");

  const int Gap = 8;
  for (QVector<CAccountInfoMenuItem>::iterator it = m_Menu.begin();
       it != m_Menu.end(); it++) {
    if (it->m_nID > 0) {
      if (it->m_nID == m_nIDHover) {
        painter->fillRect(it->m_rect, brBackHover);
      } else if (it->m_nID == m_nIDSelect) {
        painter->fillRect(it->m_rect, brBackSelect);
      }

      QRect textRect = it->m_rect;
      textRect.adjust(Gap, 0, 0, 0);
      QColor brTemp = (it->m_nID == m_nIDSelect ? brText : brFront);
      painter->setPen(QPen(brTemp));
      QFont font("Microsoft YaHei");
      font.setPixelSize(12);
      painter->setFont(font);
      painter->drawText(textRect, it->m_strTitle,
                        QTextOption(Qt::AlignLeft | Qt::AlignVCenter));

      if (it->m_SubMenu.size() > 0) {
        for (QVector<CAccountInfoMenuItem>::iterator it2 =
                 it->m_SubMenu.begin();
             it2 != it->m_SubMenu.end(); it2++) {
          if (it2->m_nID > 0) {
            if (it2->m_nID == m_nIDHover) {
              painter->fillRect(it2->m_rect, brBackHover);
            } else if (it2->m_nID == m_nIDSelect) {
              painter->fillRect(it2->m_rect, brBackSelect);
            }

            textRect = it2->m_rect;
            textRect.adjust(Gap * 3, 0, 0, 0);
            QColor brTemp = (it2->m_nID == m_nIDSelect ? brText : brFront);
            painter->setPen(QPen(brTemp));
            painter->drawText(textRect, it2->m_strTitle,
                              QTextOption(Qt::AlignLeft | Qt::AlignVCenter));
          }
        }
      }
    }
  }
}

void CAccountInfoMenu::Resize(int x, int y, int nWidth, int nHeight) {
  m_Rect = QRect(x, y + 28, x + nWidth, y + 28 + nHeight);
  int nMenuHeight = 24;
  int nGap = 8;

  QRect rcTemp = m_Rect;
  rcTemp.setLeft(x);
  rcTemp.setRight(x + nWidth);

  for (QVector<CAccountInfoMenuItem>::iterator it = m_Menu.begin();
       it != m_Menu.end(); it++) {
    if (it->m_nID > 0) {
      rcTemp.setBottom(rcTemp.top() + nMenuHeight);
      it->m_rect = rcTemp;
      rcTemp.setTop(rcTemp.bottom() + nGap);

      if (it->m_SubMenu.size() > 0) {
        for (QVector<CAccountInfoMenuItem>::iterator it2 =
                 it->m_SubMenu.begin();
             it2 != it->m_SubMenu.end(); it2++) {
          if (it2->m_nID > 0) {
            rcTemp.setBottom(rcTemp.top() + nMenuHeight);
            it2->m_rect = rcTemp;
            rcTemp.setTop(rcTemp.bottom() + nGap);
          }
        }
      }
    }
  }
}

int CAccountInfoMenu::OnMouseMove(QPoint pt) {
  for (QVector<CAccountInfoMenuItem>::iterator it = m_Menu.begin();
       it != m_Menu.end(); it++) {
    if (it->m_nID > 0) {
      if (it->m_rect.contains(QPoint(pt))) {
        if (m_nIDHover != it->m_nID) {
          m_nIDHover = it->m_nID;
          return m_nIDHover;
        } else {
          return -1;
        }
      }
      for (QVector<CAccountInfoMenuItem>::iterator it2 = it->m_SubMenu.begin();
           it2 != it->m_SubMenu.end(); it2++) {
        if (it2->m_rect.contains(QPoint(pt))) {
          if (m_nIDHover != it2->m_nID) {
            m_nIDHover = it2->m_nID;
            return m_nIDHover;
          } else {
            return -1;
          }
        }
      }
    }
  }
  if (m_nIDHover > 0) {
    m_nIDHover = 0;
    return m_nIDHover;
  }
  return -1;
}

int CAccountInfoMenu::OnLButtonDown(int x, int y) {
  QPoint pt(x, y);
  m_nIDPos = -1;
  for (QVector<CAccountInfoMenuItem>::iterator it = m_Menu.begin();
       it != m_Menu.end(); it++) {
    if (it->m_nID > 0) {
      if (it->m_rect.contains(QPoint(pt))) {
        m_nIDPos = it->m_nID;
        m_nIDSelect = it->m_nID;
        return m_nIDPos;
      }
      for (QVector<CAccountInfoMenuItem>::iterator it2 = it->m_SubMenu.begin();
           it2 != it->m_SubMenu.end(); it2++) {
        if (it2->m_rect.contains(QPoint(pt))) {
          m_nIDPos = it2->m_nID;
          m_nIDSelect = it2->m_nID;
          return m_nIDPos;
        }
      }
    }
  }
  return m_nIDPos;
}

bool CAccountInfoMenu::InstMenu(QString strMenu, int nID, int nParentID) {
  CAccountInfoMenuItem item;
  item.m_strTitle = strMenu;
  item.m_nID = nID;

  bool bFound = false;
  if (nParentID > 0) {
    QVector<CAccountInfoMenuItem>::iterator it = m_Menu.begin();
    for (; it != m_Menu.end(); it++) {
      if (it->m_nID == nParentID) {
        it->m_SubMenu.push_back(item);
        bFound = true;
        break;
      }
    }
  }
  if (!bFound) {
    m_Menu.push_back(item);
  }

  return true;
}